package com.amoby.main.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amoby.base.common.Constants;
import com.amoby.base.common.Pager;
import com.amoby.base.utils.DateUtil;
import com.amoby.base.utils.MD5Encrypt;
import com.amoby.main.entity.Business;
import com.amoby.main.entity.Channel;
import com.amoby.main.entity.SysUserRoleKey;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.mapper.BusinessMapper;
import com.amoby.main.mapper.ChannelMapper;
import com.amoby.main.mapper.SysRolesMapper;
import com.amoby.main.mapper.SysUserRoleMapper;
import com.amoby.main.mapper.SysUsersMapper;
import com.amoby.main.service.ChannelService;

/**
 * @desc:渠道service
 * @author Lasing 2016-6-7 上午10:35:26
 * @version V1.0
 */
@Service
public class ChannelServiceImpl implements ChannelService {
    @Autowired
    private ChannelMapper       channelMapper;
    @Autowired
    private SysUsersMapper      sysUsersMapper;
    @Autowired
    private  SysRolesMapper     roleDao;
    @Autowired
    private SysUserRoleMapper   userRoleMapper;
    @Autowired
    private BusinessMapper      businessMapper;
    @Override
    public Pager query(Pager pager) {
        List<?> list = channelMapper.selectCountByPager(pager);
        pager.setResult(list);
        return pager;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int add(Channel channel) {
     // 创建渠道的时候同时创建商家登陆后台 的账号
        SysUsers user = new SysUsers();
        user.setUsername(channel.getUsername());
        user.setNick(channel.getChannelName());
        user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        user.setPhone(channel.getPhone());
        user.setStatus(channel.getStatus());
        user.setEmail(channel.getEmail());
        user.setCreateDate(DateUtil.getCurDate());
        user.setUpdateDate(DateUtil.getCurDate());
        sysUsersMapper.saveUser(user);
        // 分配角色
        SysUserRoleKey userRole = new SysUserRoleKey();
        Integer roleId = roleDao.getRoleIdByRoleName(Constants.ROLE_CHANNEL_NAME);
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getUserId());
        userRoleMapper.insert(userRole);
        // 保存渠道
        channel.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        channel.setCreateTime(new Date());
        channel.setUserId(user.getUserId());
        Integer result = channelMapper.insertSelective(channel);
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int upd(Channel channel) {
        List<Business> listbus = businessMapper.selectBusByChannelId(channel.getId());  //根据渠道id查询商户
        SysUsers user = new SysUsers();
        user.setUserId(channel.getUserId());
        user.setUsername(channel.getUsername());
        user.setNick(channel.getChannelName());
        user.setPhone(channel.getPhone());
        user.setStatus(channel.getStatus());
        user.setEmail(channel.getEmail());
        user.setUpdateDate(DateUtil.getCurDate());
        sysUsersMapper.updateByPrimaryKeySelective(user);
        channel.setUpdateTime(new Date());
        int count = channelMapper.updateByPrimaryKeySelective(channel);
        if(listbus.size()>0){
            businessMapper.batchUpdate(listbus);
        }
        return count;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int reset_pwd(Long id, Integer userId) {
     // 重置渠道密码的时候同时同步修改该商家登陆后台的密码 
        SysUsers user = new SysUsers();
        user.setUserId(userId);
        user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        user.setUpdateDate(DateUtil.getCurDate());
        sysUsersMapper.updateByPrimaryKeySelective(user);
        // 重置商家密码
        Channel channel = new Channel();
        channel.setId(id);
        channel.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        return channelMapper.updateByPrimaryKeySelective(channel);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int del(Long id, Integer userId) {
     // 删除商家的时候 同步删除改商家的权限和登陆用户
        sysUsersMapper.deleteByPrimaryKey(userId);
        userRoleMapper.deleteByUserId(userId);
        return channelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int checkUserNameById(String username, Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", username);
        map.put("id", id);
        return channelMapper.checkUserNameById(map);
    }
    @Override
    public int checkChannelName(String channelName, Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("channelName", channelName);
        map.put("id", id);
        return channelMapper.checkChannelName(map);
    }
    
    @Override
    public Channel getChannelByUserId(Integer userId) {
        return channelMapper.getChannelByUserId(userId);
    }

    @Override
    public Channel getChannelByChannelName(String channelName) {
        return channelMapper.getChannelByChannelName(channelName);
    }


    
}
